package org.ansel.tools.eza;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class SendEmailDemo {
	public static void main(String[] args) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", "unp-stmp.int.sfdc.com.cn"); //// 设置QQ邮件服务器
        prop.setProperty("mail.transport.protocol", "smtp"); // 邮件发送协议
        prop.setProperty("mail.smtp.auth", "true"); // 需要验证用户名密码
        final String smtpPort = "1080";
        prop.setProperty("mail.smtp.port", smtpPort);
//        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
//        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
//        prop.setProperty("mail.smtp.socketFactory.port", smtpPort);

        // 关于QQ邮箱，还要设置SSL加密，加上以下代码即可
//        MailSSLSocketFactory sf = new MailSSLSocketFactory();
//        sf.setTrustAllHosts(true);
//        prop.put("mail.smtp.ssl.enable", "true");
//        prop.put("mail.smtp.ssl.socketFactory", sf);

        //使用JavaMail发送邮件的5个步骤

        //1、创建定义整个应用程序所需的环境信息的 Session 对象
        Session session = Session.getDefaultInstance(prop, new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                //发件人邮件用户名、授权码
                return new PasswordAuthentication("cWOgWKR7", "033e62a3e4ad48998a7987e80862358d");
            }
        });


        //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);

        //2、通过session得到transport对象
        Transport ts = session.getTransport();

        //3、使用邮箱的用户名和授权码连上邮件服务器
        ts.connect("unp-stmp.int.sfdc.com.cn", "cWOgWKR7", "033e62a3e4ad48998a7987e80862358d");
        //4、创建邮件

        //创建邮件
        MimeMessage message = new MimeMessage(session);

        // 设置邮件的基本信息
        //发件人
        message.setFrom(new InternetAddress("eapmp@sf-express.com"));
        //收件人
        String toAddrs="jian.zhang6@sfmail.sf-express.com,hustzhangj@163.com";
        String ccAddrs="412144103@qq.com";
        message.setRecipients(Message.RecipientType.TO,  InternetAddress.parse(toAddrs));
        message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccAddrs));
        //邮件标题
        message.setSubject("PMS带链接的邮件");

        // 准备邮件数据

        // 准备正文数据
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("确认提醒：\";移动交互-位置测试-测试-bim1-sit-2020-07-06(正常)\"需要您的确认，请查阅\";,mailrequestname:\"确认提醒：\";(<a  target='_blank' href=\"http://acppms.sit.sf-express.com:10099/login/Login.jsp?gopage=/workflow/request/ViewRequest.jsp?requestid=5843\" >移动交互-位置测试-测试-bim1-sit-2020-07-06</a>)\"需要您的确认，请查阅", "text/html;charset=UTF-8");

        // 描述数据关系
        MimeMultipart mm = new MimeMultipart();
        mm.addBodyPart(text);
        mm.setSubType("related");

        //设置到消息中，保存修改
        message.setContent(mm);
        message.saveChanges();

        //5.发送邮件
        ts.sendMessage(message, message.getAllRecipients());
        ts.close();
    }
}
